package day02;

import org.w3c.dom.NodeList;

/**
 * @author Jetwang
 * @version 1.0
 * @since 2023/3/2 18:40
 */
public class LinkedListPractice01 {
    public static void main(String[] args) {
        ListNode o5 = new ListNode(5, null);
        ListNode o4 = new ListNode(4, o5);
        ListNode o3 = new ListNode(3, o4);
        ListNode o2 = new ListNode(2, o3);
        ListNode o1 = new ListNode(1, o2);
        // ListNode o1 = null;

        System.out.println(o1);
        // ListNode newList = new LinkedListPractice01().reverseList01(o1);
        // ListNode newList = new LinkedListPractice01().reverseList02(o1);
        // ListNode newList = new LinkedListPractice01().reverseList03(o1);
        // ListNode newList = new LinkedListPractice01().reverseList04(o1);
        ListNode newList = new LinkedListPractice01().reverseList05(o1);
        System.out.println(newList);
    }

    /**
     * 反转链表01
     * @param head
     * @return
     */
    public ListNode reverseList01(ListNode head) {
        ListNode n1 = null;
        ListNode p = head;
        while(p != null) {
            n1 = new ListNode(p.val, n1);
            p = p.next;
        }
        return n1;
    }


    static class List {
        ListNode head;

        public List(ListNode head) {
            this.head = head;
        }

        public void addFirst(ListNode first) {
            first.next = head;
            head = first;
        }

        public ListNode removeFirst() {
            ListNode first = head;
            if (first != null) {
                head = first.next;
            }
            return first;
        }
    }

    /**
     * 反转链表02
     * @param head
     * @return
     */
    public ListNode reverseList02(ListNode head) {
        List list1 = new List(head);
        List list2 = new List(null);
        while(true) {
            ListNode first = list1.removeFirst();
            if (first == null) {
                break;
            }
            list2.addFirst(first);
        }
        return list2.head;
    }

    /**
     * 反转链表03
     * @param p
     * @return
     */
    public ListNode reverseList03(ListNode p) {
        if (p == null || p.next == null) {
            return p;
        }
        ListNode last = reverseList03(p.next);
        p.next.next = p;
        p.next = null;
        return last;
    }

    /**
     * 反转链表04
     * @param o1
     * @return
     */
    public ListNode reverseList04(ListNode o1) {
        while(o1 == null || o1.next == null) {
            return o1;
        }
       ListNode p = o1.next;
       ListNode n1 = o1;
       while(p != null) {
           o1.next = p.next;
           p.next = n1;
           n1 = p;
           p = o1.next;
       }
       return n1;
    }

    /**
     * 反转链表05
     * @param head
     * @return
     */
    public ListNode reverseList05(ListNode head) {
        ListNode newList = null;
        while(head != null) {
            ListNode p = head.next;
            head.next = newList;
            newList = head;
            head = p;
        }
        return newList;
    }
}
